<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      h2,
      p {
        margin: 0;
      }
      .box {
        margin: 30px auto;
        width: 300px;
      }
      #user,
      #message {
        box-sizing: border-box;
        width: 300px;
        border: 1px solid #000;
        padding: 10px;
        height: 100px;
        resize: none;
        display: block;
      }
      #user {
        padding: 0 10px;
        height: 30px;
        margin-bottom: 10px;
      }
      #btn {
        display: block;
        margin: 10px auto;
        width: 60%;
        height: 40px;
      }
      #list {
        border-top: 1px solid #000;
        padding: 10px 0 0;
        margin: 0;
        list-style: none;
      }
      #list li {
        position: relative;
        font: 14px/30px "宋体";
        border-bottom: 1px solid #000;
        padding: 0 15px;
        overflow: hidden;
      }
      .clos {
        position: absolute;
        right: 5px;
        top: 10px;
      }
      .clos2 {
        position: absolute;
        right: 5px;
        top: 30px;
      }
      .replay {
        position: absolute;
        right: 5px;
        top: 35px;
      }
      .mask {
        width: 100vw;
        height: 100vh;
        background: black;
        opacity: 0.3;
        position: absolute;
        left: 0;
        top: 0;
        display: none;
      }
      .comfirm {
        width: 200px;
        height: 120px;
        background: white;
        text-align: center;
        position: absolute;
        left: 50%;
        top: 30%;
        border-radius: 5px;
        transform: translate(-50%, -50%);
        overflow: hidden;
        display: none;
      }
      .comfirm button {
        /* position: absolute; */
        /* right: 10px; */
        float: right;
        margin-top: 20px;
        margin-right: 10px;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <input type="text" id="user" value="小明" />
      <textarea id="message">我就是传说中的无敌小明</textarea>
      <button id="btn">提交留言</button>
      <ul id="list">
        <li style="height: auto">
          <h2 class="user">小王说:</h2>
          <p>呼叫无敌小明</p>
          <a href="javascript:;" class="clos">删除</a>
        </li>
        <li style="height: auto">
          <h2 class="user">老师说:</h2>
          <p>滚出去</p>
          <a href="javascript:;" class="clos">删除</a>
        </li>
      </ul>
    </div>
    <script>
      // 一、实现一个添加功能；

      let addBtn = document.querySelector("#btn");
      let userEle = document.querySelector("#user");
      let messageEle = document.querySelector("#message");
      getData();

      function getData() {
        let xhr2 = new XMLHttpRequest();
        xhr2.open("get", "/getmessage");
        xhr2.send();
        xhr2.onload = function () {
          if (xhr2.status >= 200 && xhr2.status < 300) {
            console.log(xhr2.responseText);
            let res = JSON.parse(xhr2.responseText);
            console.log(res);

            if (res.status == 1) {
              renderDom(res.messageData);
            }
          }
        };
      }

      function renderDom(data) {
        let ulEle = document.querySelector("#list");
        ulEle.innerHTML = "";
        data.forEach((item) => {
          let liEle = document.createElement("li");
          liEle.style.height = "auto";
          liEle.innerHTML = `<h2 class="user">${item.username}说:</h2>
                            <p>${item.message}</p>
                            <a href="javascript:;" 
                            class="clos"
                            idAttr="${item._id}"
                            >删除</a>`;

          let delBtn = liEle.querySelector(".clos");
          delBtn.onclick = function () {
            let _id = this.getAttribute("idAttr");
            let xhr3 = new XMLHttpRequest();
            xhr3.open("get", `/delmessage?_id=${_id}`);
            xhr3.send();
            xhr3.onload = function () {
              if (xhr3.status >= 200 && xhr3.status < 300) {
                console.log(xhr.responseText);
                let result = JSON.parse(xhr3.responseText);
                if (result.status == 1) {
                  console.log('脑阔痛')
                  getData();
                }
              }
            };
          };

          ulEle.appendChild(liEle);
        });
      }
      addBtn.onclick = function () {
        let username = userEle.value;
        let message = messageEle.value;
        let xhr = new XMLHttpRequest();
        xhr.open("get", `/adduser?username=${username}&message=${message}`);
        xhr.send();
        xhr.onload = function () {
          if (xhr.status >= 200 && xhr.status < 300) {
            console.log(xhr.responseText);
            getData();
          }
        };
      };
    </script>
  </body>
</html>
